//
//  BDHomeModel.m
//  BadBoy
//
//  Created by wen on 2017/3/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDHomeModel.h"

@implementation BDHomeModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"repostCount":@"statistic.repost",
             @"commentCount":@"statistic.comment",
             @"praiseCount":@"statistic.praise",
             @"sharedCount":@"statistic.shared",
             @"my_id":@"id",
             @"created_at":@"posted_at"};
}

//+ (NSDictionary *)mj_objectClassInArray{
//    return @{@"user":[BDUserModel class],
//             @"repost":[BDHomeModel class]};
//}

- (CGFloat)cellHeight{
    CGFloat height = 7 + 40 + 8 + self.contentHeight;
    if (self.needOpen == YES && self.isDetail == NO) {
        height = height + 24;
    }else{
        height = height + 10;
    }
    //图片部分
    if (self.imageHeight != 0) {
        height += (self.imageHeight + 10);
    }
    //转发部分
    if (self.forwardHeight != 0) {
        height += (self.forwardHeight + 10);
    }
    if (self.isDetail == YES) {
        if (self.isPurchase == NO && self.haveMeracten == YES && self.price != 0) {
            height += (self.merachantHeight + 10);
        }
        return height;
    }else {
        if (self.merachantHeight != 0) {
            height += (self.merachantHeight + 10);
        }
    }
    return height + 36 + 10;
}

- (BOOL)needOpen{
    CGFloat contenTextHeight = [self getyytextHeightWithText:self.post];
    CGFloat vaildHeight = [self getyytextHeightWithText:@"1\n2\n3\n4\n5\n6"];
    return vaildHeight < contenTextHeight;
}

- (CGFloat)contentHeight{
    CGFloat contenTextHeight = [self getyytextHeightWithText:self.post];
    CGFloat vaildHeight = [self getyytextHeightWithText:@"1\n2\n3\n4\n5\n6"];
    if (_isOpen == YES || _isDetail == YES) {
        return contenTextHeight;
    }
    return MIN(vaildHeight, contenTextHeight);
}

- (CGFloat)getyytextHeightWithText:(NSString *)text{
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: text];
    one.lineSpacing = 5;
    one.font = [UIFont systemFontOfSize:15];
    
    YYTextContainer *container = [YYTextContainer new];
    container.insets = UIEdgeInsetsZero;
    container.size = CGSizeMake(kBDScreenWidth - 20, CGFLOAT_MAX);
    container.maximumNumberOfRows = 0;
//    YYTextLinePositionSimpleModifier *modifier = [YYTextLinePositionSimpleModifier new];
//    modifier.fixedLineHeight = 20;
//    container.linePositionModifier= modifier;
    
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:one];
//    [layout lineIndexForPoint:CGPointMake(10,10)];
//    [layout closestLineIndexForPoint:CGPointMake(10,10)];
//    [layout closestPositionToPoint:CGPointMake(10,10)];
//    [layout textRangeAtPoint:CGPointMake(10,10)];
//    [layout rectForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]];
//    [layout selectionRectsForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]];
    
    
    return layout.textBoundingSize.height;
}

- (CGFloat)imageHeight{
    if (self.images.count == 0) {
        return 0;
    }else if (self.images.count == 1) {
        return 150;
    }
    CGFloat side = 10;
    CGFloat space = 5;
    CGFloat photoWidth = ([UIScreen mainScreen].bounds.size.width - space * 2 - side * 2) / 3.0;
    if (self.images.count % 3 == 0) {
        return (space + photoWidth) * (self.images.count / 3);
    }else{
        return (space + photoWidth) * (self.images.count / 3 + 1);
    }
}

- (CGFloat)forwardHeight{
    if (self.repost == nil) {
        return 0;
    }
    NSString *str = [NSString stringWithFormat:@"@%@：%@", self.repost.nickname, self.repost.post];
    UIFont *font = [UIFont systemFontOfSize:15];
    CGFloat textHeight = [str heightWithFont:font width:kBDScreenWidth - 20];
    
    return 10 + textHeight + 5 + self.repost.imageHeight + 10;
}

- (CGFloat)merachantHeight{
    if (self.haveMeracten) {
        return 95;
    }
    return 0;
}

- (BOOL)free{
    if (self.price == 0 || (self.repost != nil && self.repost.price == 0)) {
        return YES;
    }
    return NO;
}

- (BOOL)haveShopInfo{
    if ([self.shop isNotEmpty] || [self.address isNotEmpty] || [self.contact isNotEmpty]) {
        return YES;
    }
    return NO;
}

- (BOOL)haveMeracten{

    //如果不免费，那么就肯定是YES
    if (self.price != 0 || (self.repost != nil && self.repost.price != 0)) {
        return YES;
    }else{
        //如果免费，那么就判断shop是否为空
        if ([self haveShopInfo] || (self.repost != nil && [self.repost haveShopInfo])) {
            return YES;
        }
    }
    return NO;
}

//这是决定是购买了，还是没有购买，前提是肯定不免费的才会有这样的判断
- (BOOL)isPurchase{
    //首先应该判断哪一部分收费，然后判断这一部分是
    BDUserModel *user = [BDUserModel getCurrentUser];
    if (user.vip_level != 0) {
        return YES;
    }else {
        if (self.price != 0) {
            if (self.user.user_id == user.user_id || self.purchase.count != 0) {
                return YES;
            }else{
                return NO;
            }
        }else {
            if (self.repost.user.user_id == user.user_id || self.repost_purchase.count != 0) {
                return YES;
            }else {
                return NO;
            }
        }
    }
}

- (BDUserModel *)user{
    if (!_user) {
        self.user = [[BDUserModel alloc] init];
        _user.nickname = self.nickname;
        _user.user_id = self.user_id;
        _user.cover = self.cover;
        _user.vip_level = self.vip_level;
    }
    return _user;
}

@end
